package com.example;


import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.Arrays;
import java.util.concurrent.CompletableFuture;


/**
 * https://openjdk.org/jeps/102
 */
class JEP102 {

    @Test
    public void testProcessHandle() {
        final ProcessHandle processHandle = ProcessHandle.current();
        final ProcessHandle.Info info = processHandle.info();
        System.out.println("Process info =>");
        System.out.format("PID: %s%n", processHandle.pid());
        info.arguments().ifPresent(args -> System.out.format("Arguments: %s%n", Arrays.toString(args)));
        info.command().ifPresent(command -> System.out.format("Command: %s%n", command));
        info.commandLine()
                .ifPresent(commandLine -> System.out.format("Command line: %s%n", commandLine));
        info.startInstant()
                .ifPresent(startInstant -> System.out.format("Start time: %s%n", startInstant));
        info.totalCpuDuration()
                .ifPresent(cpuDuration -> System.out.format("CPU time: %s%n", cpuDuration));
        info.user().ifPresent(user -> System.out.format("User: %s%n", user));
    }



    @Test
    public void testControlProcess() throws IOException, InterruptedException {
        final ProcessBuilder processBuilder = new ProcessBuilder("ffmpeg" ,"-i" ,"D:/zhaochao.mp4",  "D:/1.mp3").inheritIO();
        ProcessHandle processHandle = processBuilder.start().toHandle();
        // 通过CompletetableFuture 在进程结束时运行某个命令
        CompletableFuture<ProcessHandle> cf = processHandle.onExit();
        ProcessHandle.Info info = processHandle.info();

        System.out.println("进程命令：" + info.command());
        System.out.println("进程参数：" + info.arguments());
        System.out.println("进程启动时间： " + info.startInstant());
        System.out.println("进程积计运行时间：" + info.totalCpuDuration());
        // 通过CompletetableFuture 在进程结束时运行某个命令

        cf.thenRunAsync(() -> System.out.println("程序退出"));
        Thread.sleep(5000);
    }

}
